package com.ruubypay.biz.mvc.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ruubypay.biz.mvc.api.model.request.*;
import com.ruubypay.biz.mvc.api.model.response.OutGoingDetail;
import com.ruubypay.biz.mvc.api.model.response.ProcessDetails;
import com.ruubypay.biz.mvc.api.service.ProcessTaskService;
import com.ruubypay.biz.mvc.api.validation.RequestValidation;
import com.ruubypay.web.common.code.ResultCode;
import com.ruubypay.web.common.code.ResultEntity;
import com.ruubypay.web.common.page.DataGrid;
import com.ruubypay.web.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 与工作流任务相关的ResutFulAPI
 * @author chenhaiyang
 */
@Controller
@RequestMapping(value="/workflow/api/task/",produces="application/json;charset=UTF-8")
public class ProcessTaskController {

    private static Logger logger = LoggerFactory.getLogger(ProcessTaskController.class);
    /**
     * 获取当前用户的历史任务
     */
    @Resource
    private ProcessTaskService processTaskService;

    private RequestValidation validation = new RequestValidation();

    /**
     * 查询用户的待办任务
     * @return 返回json实体类
     */
    @ResponseBody
    @RequestMapping(value="/list",method = RequestMethod.POST)
    public String list(@RequestBody QueryTask queryTaskList){

        if(StringUtils.isBlank(queryTaskList.getUserName())){
            return JSON.toJSONString(new DataGrid(0,new ArrayList<>()),SerializerFeature.WriteMapNullValue);
        }
        DataGrid modelPage =processTaskService.getUserTaskActive(queryTaskList);
        return JSON.toJSONString(modelPage,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 查询用户的已完成的任务列表
     * @return 返回json实体类
     */
    @ResponseBody
    @RequestMapping(value="/listfinished",method = RequestMethod.POST)
    public String listfinished(@RequestBody QueryTask queryTaskList){

        if(StringUtils.isBlank(queryTaskList.getUserName())){
            return JSON.toJSONString(new DataGrid(0,new ArrayList<>()),SerializerFeature.WriteMapNullValue);
        }
        DataGrid modelPage =processTaskService.getUserTaskFinisned(queryTaskList);
        return JSON.toJSONString(modelPage,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 发起流程
     * @param startProcess 创建任务请求VO类
     * @return 返回任务发起结果
     */
    @ResponseBody
    @RequestMapping(value="/create",method = RequestMethod.POST)
    public String create(@RequestBody StartProcess startProcess){
        ResultEntity<String> resultEntity = new ResultEntity<>(ResultCode.COMMON_SUCCESS);
        if(!validation.validateStartProcess(startProcess,resultEntity)){
            return JSON.toJSONString(resultEntity,SerializerFeature.WriteMapNullValue);
        }
        try {
            processTaskService.startProcessBykey(startProcess);
        } catch (Exception e) {
           logger.error(e.getMessage(),e);
           resultEntity.setCodeMsg(ResultCode.PROCESS_STARTFAIL);
        }
        return JSON.toJSONString(resultEntity, SerializerFeature.WriteMapNullValue);
    }


    /**
     * 签收任务
     * @param claimTask 签收任务
     * @return 返回任务签收结果
     */
    @ResponseBody
    @RequestMapping(value="/claim",method = RequestMethod.POST)
    public String claim(@RequestBody ClaimTask claimTask){
        ResultEntity<String> resultEntity = new ResultEntity<>(ResultCode.COMMON_SUCCESS);
        if(!validation.validateClaimTask(claimTask,resultEntity)){
            return JSON.toJSONString(resultEntity,SerializerFeature.WriteMapNullValue);
        }
        try {
            processTaskService.claimTask(claimTask);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            resultEntity.setCodeMsg(ResultCode.TASK_CIALIM_FAIL);
        }
        return JSON.toJSONString(resultEntity,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 任务指派，指的是当前任务的办理人不想办理，将任务指派给别人
     * @param assignedTask 指派任务请求类
     * @return 返回任务指派操作结果
     */
    @ResponseBody
    @RequestMapping(value="/assigne",method = RequestMethod.POST)
    public String assigne(@RequestBody AssignedTask assignedTask){
        ResultEntity<String> resultEntity = new ResultEntity<>(ResultCode.COMMON_SUCCESS);
        if(!validation.validateAssigneTask(assignedTask,resultEntity)){
            return JSON.toJSONString(resultEntity,SerializerFeature.WriteMapNullValue);
        }
        try {
            processTaskService.assigneeTask(assignedTask);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            resultEntity.setCodeMsg(ResultCode.TASK_ASSIGNEE_FAIL);
        }
        return JSON.toJSONString(resultEntity,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 完成任务
     * @param submitTask 提交任务的VO类
     * @return 返回任务完成结果
     */
    @ResponseBody
    @RequestMapping(value="/submit",method = RequestMethod.POST)
    public String submit(@RequestBody SubmitTask submitTask){
        ResultEntity<String> resultEntity = new ResultEntity<>(ResultCode.COMMON_SUCCESS);
        if(!validation.validateSubmitTask(submitTask,resultEntity)){
            return JSON.toJSONString(resultEntity,SerializerFeature.WriteMapNullValue);
        }
        try {
            processTaskService.sumbitTask(submitTask);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            resultEntity.setCodeMsg(ResultCode.TASK_SUBMIT_FAIL);
        }
        return JSON.toJSONString(resultEntity,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 获取任务的流程跟踪图
     * @param queryProcessInfo 查询流程任务的跟踪图
     * @return 返回任务完成结果
     */
    @ResponseBody
    @RequestMapping(value="/processinfo",method = RequestMethod.POST)
    public String processinfo(@RequestBody QueryProcessInfo queryProcessInfo){
        ResultEntity<ProcessDetails> resultEntity = new ResultEntity<>(ResultCode.COMMON_SUCCESS);
        if(!validation.validateProcessInfoQuery(queryProcessInfo,resultEntity)){
            return JSON.toJSONString(resultEntity,SerializerFeature.WriteMapNullValue);
        }
        try {
            ProcessDetails processDetails= processTaskService.queryProcessInfo(queryProcessInfo);
            resultEntity.setResData(processDetails);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            resultEntity.setCodeMsg(ResultCode.QUERY_PROCESS_INFO_FAIL);
        }
        return JSON.toJSONString(resultEntity,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 获取当前任务的连线，用于前台生成按钮
     * @param queryOutGoingLines 查询连线的请求VO类
     * @return 返回查询的结果
     */
    @ResponseBody
    @RequestMapping(value="/querylines",method = RequestMethod.POST)
    public String querylines(@RequestBody QueryOutGoingLines queryOutGoingLines){
        ResultEntity<List<OutGoingDetail>> resultEntity = new ResultEntity<>(ResultCode.COMMON_SUCCESS);
        if(!validation.validateOutGoingsLinesQuery(queryOutGoingLines,resultEntity)){
            return JSON.toJSONString(resultEntity);
        }
        try {
            List<OutGoingDetail> outGoingDetails= processTaskService.getOutComingLines(queryOutGoingLines);
            resultEntity.setResData(outGoingDetails);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            resultEntity.setCodeMsg(ResultCode.QUERY_OUT_COMING_LINE_FAIL);
        }
        return JSON.toJSONString(resultEntity,SerializerFeature.WriteMapNullValue);
    }


}
